TestRouter   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 11
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 9
dl 0
loc 11
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A test 0 3 1
A error 0 4 1
1
import { flushPromises } from '@test/utils/testUtils';
2
import * as http from 'http';
3
import { Request, Response } from 'express';
4
import request from 'supertest';
5
import ExpressBeans from '@/core/ExpressBeans';
6
import { Route, RouterBean } from '@/main';
7
import { Executor } from '@/core/Executor';
8
9
describe('Error Handling integration tests', () => {
10
  let server: http.Server;
11
  let application: ExpressBeans;
12
  beforeEach(() => {
13
    jest.resetModules();
14
  });
15
16
  afterEach(() => {
17
    server.close();
18
    Executor.stopLifecycle();
19
  });
20
21
  test('error handling on synchronous routes', async () => {
22
    // GIVEN
23
    @RouterBean('/test')
24
    class TestRouter {
25
      @Route('GET', '/42')
26
      test(_req: Request, res: Response) {
27
        res.send('42 is the answer');
28
      }
29
30
      @Route('GET', '/error')
31
      error(_req: Request, _res: Response) {
32
        throw new Error('ops!');
33
      }
34
    }
35
    application = new ExpressBeans({ listen: false, routerBeans: [TestRouter] });
36
    await flushPromises();
37
    server = application.listen(3001);
38
    await flushPromises();
39
40
    // WHEN
41
    await request(server).get('/test/error').expect(500);
42
43
    // THEN
44
    const { text } = await request(server).get('/test/42').expect(200);
45
    expect(text).toBe('42 is the answer');
46
  });
47
48
  test('error handling on asynchronous routes', async () => {
49
    // GIVEN
50
    @RouterBean('/test')
51
    class TestRouter {
52
      @Route('GET', '/42')
53
      test(_req: Request, res: Response) {
54
        res.send('42 is the answer');
55
      }
56
57
      @Route('GET', '/error')
58
      async error(_req: Request, _res: Response) {
59
        throw new Error('ops!');
60
      }
61
    }
62
    application = new ExpressBeans({ listen: false, routerBeans: [TestRouter] });
63
    await flushPromises();
64
    server = application.listen(3001);
65
    await flushPromises();
66
67
    // WHEN
68
    await request(server).get('/test/error').expect(500);
69
70
    // THEN
71
    const { text } = await request(server).get('/test/42').expect(200);
72
    expect(text).toBe('42 is the answer');
73
  });
74
});
75